package com.example.fw;

import java.util.List;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import com.example.tests.ContactData;
import com.example.utils.SortedListOf;

public class ContactHelper extends WebDriverHelperBase{
	
	

	public ContactHelper(ApplicationManager manager) {
		super(manager);
	}

	private SortedListOf<ContactData> cachedContacts;
	
	public SortedListOf<ContactData> getContacts() {
		
		if (cachedContacts == null) {
			  rebuildContactCache(); 
		}
		return cachedContacts; 
	}
	
	private void rebuildContactCache(){	
		cachedContacts = new SortedListOf<ContactData>();
		manager.navigateTo().mainPage();
		List<WebElement> rows = driver.findElements(By.xpath("//tr[@name='entry']"));
		for (WebElement row : rows) {
			List<WebElement> cells = row.findElements(By.tagName("td")); 
			String lastName = cells.get(1).getText();
		    String firstName= cells.get(2).getText();
		    String email = cells.get(3).getText();
		    String phoneHome= cells.get(4).getText();
		    ContactData contact =new ContactData()
		    	.withFirstName(firstName)
		    	.withLastName(lastName)
		    	.withEmail(email)
		    	.withPhoneHome(phoneHome); 
		    cachedContacts.add(contact); 
		    }
}

	public ContactHelper createContact(ContactData contact) {
		manager.navigateTo().contactPage();
		fillContactForm(contact);
		submitContactCreation();
		manager.navigateTo().homePage();
		rebuildContactCache(); 
		return this;
	}
	

	public ContactHelper modifyContact(int index, ContactData contact) {
		fillContactForm(contact);
		submitContactModification();
	    manager.navigateTo().homePage();
	    rebuildContactCache(); 
		return this; 
	}
	

	
	public ContactHelper deleteContact(int index) {
		selectContactByIndex(index);
		submitContactDeletion();	
		manager.navigateTo().homePage();
		rebuildContactCache(); 
		return this;
	}
	
	


//-----------------------------------------------------------------------------------------------------------------------------
	public ContactData getContactDataFromUI(int index) {
		selectContactByIndex(index);
		ContactData contactData = new ContactData()
			.withFirstName(driver.findElement(By.name("firstname")).getAttribute("value"))
			.withLastName(driver.findElement(By.name("lastname")).getAttribute("value"))
			.withAddress(driver.findElement(By.name("address")).getText())
			.withPhoneHome(driver.findElement(By.name("home")).getAttribute("value"))
			.withPhoneMobile(driver.findElement(By.name("mobile")).getAttribute("value"))
			.withPhoneWork(driver.findElement(By.name("work")).getAttribute("value"))
			.withEmail(driver.findElement(By.name("email")).getAttribute("value"))
			.withDayOfBirth(driver.findElement(By.xpath("//*[@id='content']//select[@name='bday']/option[@selected='selected']")).getAttribute("value"))
			.withMonthOfBirth(driver.findElement(By.xpath("//*[@id='content']//select[@name='bmonth']/option[@selected='selected']")).getAttribute("value"))
			.withYearOfBirth(driver.findElement(By.name("byear")).getAttribute("value"));
		return contactData;
	}
	
	public ContactHelper fillContactForm(ContactData contact) {
	    type(By.name("firstname"), contact.getFirstName());
	    type(By.name("lastname"), contact.getLastName());
	    type(By.name("address"), contact.getAddress());
		type(By.name("home"), contact.getPhoneHome());
		type(By.name("mobile"), contact.getPhoneMobile());
		type(By.name("work"), contact.getPhoneWork());
		type(By.name("email"), contact.getEmail());
		selectByText(By.name("bday"), contact.getDayOfBirth());
		selectByText(By.name("bmonth"), contact.getMonthOfBirth());
		type(By.name("byear"), contact.getYearOfBirth());
		return this; 
	}
	
	public ContactHelper submitContactCreation() {
		click(By.name("submit"));
		cachedContacts= null;
		return this;
	}
	
	public ContactHelper submitContactDeletion() {
		click(By.xpath("//*[@id='content']/form[contains(@action,'delete.php')]/input[@name='update']"));
		cachedContacts = null;
		return this;
	}

	public ContactHelper selectContactByIndex(int index) {
		click(By.xpath("//*[@id='maintable']//tr[@name='entry'][" + (index+1) + "]//a[starts-with(@href, 'edit.php')]"));
		return this;
	}

	public ContactHelper submitContactModification() {
		click(By.xpath("//*[@id='content']/form[contains(@action,'edit.php')]/input[@name='update']"));
		cachedContacts= null;
		return this;
	}





}
